<template>
  <div class="category">
    <div class="header">
      <div class="add">
        <el-button type="primary" @click="toAddCategory">新增</el-button>
      </div>
      <div class="form">
        <el-form>
          <el-form-item>
            <el-input v-model="keywords" placeholder="请输入类别编号或名称" clearable>
              <el-button slot="append" icon="el-icon-search" @click="getCategoryByKeyWords"/>
            </el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-table :data="list" border fit>
      <el-table-column label="编号" align="center">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          {{ scope.row.status===1? '正常': '禁用' }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.created_at }}
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.updated_at }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="200">
        <template slot-scope="scope">
          <el-button type="primary" @click="toCategoryDetail(scope.row.id)">详情</el-button>
          <el-button type="warning" @click="deleteCategory(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
    :total="total"
    :page.sync="listQuery.page"
    :limit.sync="listQuery.limit"
    @pagination="getList" />
  </div>
</template>
<script>
import categoryApi from '@/api/category'
import Pagination from '@/components/Pagination'
export default {
  components: {
    Pagination
  },
  data() {
    return {
      list: [],
      keywords: '',
      total: 0,
      listQuery: {
        page: 1,
        limit: 10
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取商品类别
    async getList(e) {
      const query = {}
      if(e == undefined) {
        query.page = this.listQuery.page - 1,
        query.limit = this.listQuery.limit
      }
      else {
        query.page = e.page - 1,
        query.limit = e.limit
      }
      const result = await categoryApi.getCategorys(query)
      this.list = result.data.rows
      this.total = result.data.count
    },
    toAddCategory() {
      this.$router.push({
        name: 'AddCategory'
      })
    },
    toCategoryDetail(id) {
      this.$router.push({
        name: 'EditCategory',
        params: { id: id}
      })
    },
    // 根据搜索关键字获取商品
    async getCategoryByKeyWords() {
      if(this.keywords) {              //搜索关键字不为空
        const query = {}
        if(Number(this.keywords)) {
          query.id = this.keywords
        }else {
          query.name = this.keywords
        }
        const result = await categoryApi.getCategoryByKeyWords(query)
        if(result.code == 0) {
          this.list = []
          this.list = result.data
        }
      }else {                        // 搜索关键字为空
        this.getList()
      }
    },
    // 删除商品类别
    async deleteCategory(id) {
      const result = await categoryApi.deleteCategory(id)
      if(result.code == 0) {
        this.$message.success('删除成功')
      }else {
        this.$message.warning('删除失败')
      }
      this.reload()
    }
  },
  inject: ['reload']
}
</script>
<style lang="less" scoped>
.category {
    margin: 30px;
    position: relative;
    .header {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
    }
  }
</style>
